package com.pcbly.web.components.utils.template;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
 
public abstract class AbstractModel extends ModelPrint {

	private Class<?> clz;

	private String entity;

	private String entityP;

	private String entityL;

	private String firstEntityL;

	private String clzName;

	private String tableName; 

	public void printXml(String pathXml){
		System.out.println("<import resource=\"" + pathXml + "\"/>");
	} 

	public String readTpl(File tpl) {
		return readTpl(tpl, "UTF-8");
	}

	public AbstractModel(Class<?> clz) {
		this.clz = clz;
		this.clzName = clz.getName();
//		 System.out.println(this.clzName);
		this.entity = this.clzName.substring(this.clzName.lastIndexOf(".") + 1,
				this.clzName.length());
//		 System.out.println(this.entity);
		String[] args = this.clzName.split("\\.");
		this.entityL = args[args.length - 2];
//		 System.out.println(this.entityL);
		this.entityP = this.entity.substring(0, this.entity.length() - 4);
//		 System.out.println(this.entityP);
		this.firstEntityL =this.entityP.substring(0, 1).toLowerCase()+ this.entityP.substring(1,this.entityP.length());
//		 System.out.println(this.firstEntityL);
	}

	public abstract void execute();

	

	public void executeName(String name, String path) {
		try {
			StringBuilder sb = new StringBuilder();
			File file = new File(path);

			File fileXML = new File(Constant.getFilePath(Constant.packName,
					name));
			sb.append(readTpl(fileXML));
//			System.out.println(sb);
			
			FileUtils.writeStringToFile(file, sb.toString(), "UTF-8");
			sb.delete(0, sb.length());
			if (file != null)
				file = null;
			if (fileXML != null)
				fileXML = null;
		} catch (Exception e) {

		}
	}

	public String readTpl(File tpl, String charset) {
		String content = null;
		try {
			content = FileUtils.readFileToString(tpl, charset);
			content = content.replaceAll("\\#\\{entityL\\}", this.entityL);
			content = content.replaceAll("\\#\\{entityP\\}", this.entityP);
			content = content.replaceAll("\\#\\{clzName\\}", this.clzName);
			content = content.replaceAll("\\#\\{entity\\}", this.entity);
			content = content.replaceAll("\\#\\{tableName\\}", this.tableName);
			content = content.replaceAll("\\#\\{firstEntityL\\}",
					this.firstEntityL);

		} catch (IOException e) {
			System.out.println("read file faild. " + e.getMessage());
		}
		return content;
	}
	
	public String serviceXmlPath() {
		String packageName = "com.pcbly.web.business." + this.entityL;
		String name = this.firstEntityL + "-applicationContext-service.xml";
		return Constant.getFilePath(packageName, name);
	}

	public String servicePath() {
		String packageName = "com.pcbly.web.business." + this.entityL;
		String name = this.entityP + "Service.java";
		return Constant.getFilePath(packageName, name);
	}

	public String serviceImpPath() {
		String packageName = "com.pcbly.web.business." + this.entityL;
		String name = this.entityP + "ServiceImp.java";
		return Constant.getFilePath(packageName, name);
	}
	
	public String dwrXmlPath() {
		String packageName = "com.pcbly.web.action." + this.entityL;
		String name = this.firstEntityL + "-applicationContext-dwr.xml";
		return Constant.getFilePath(packageName, name);
	}

	public String actionXmlPath() {
		String packageName = "com.pcbly.web.action." + this.entityL;
		String name = this.firstEntityL + "-applicationContext-struts.xml";
		return Constant.getFilePath(packageName, name);
	}

	public String actionStrutsPath() {
		String packageName = "com.pcbly.web.action." + this.entityL;
		String name = this.firstEntityL + "-struts.xml";
		return Constant.getFilePath(packageName, name);
	}

	public String actionPath() {
		String packageName = "com.pcbly.web.action." + this.entityL;
		String name = this.entityP + "Action.java";
		return Constant.getFilePath(packageName, name);
	}

	public String daoXmlPath() {
		String packageName = "com.pcbly.web.persistence." + this.entityL;
		String name = this.firstEntityL + "-applicationContext-dao.xml";
		return Constant.getFilePath(packageName, name);
	}

	public String daoImplPath() {
		String packageName = "com.pcbly.web.persistence." + this.entityL;
		String name = this.entityP + "HibernateDao.java";
		return Constant.getFilePath(packageName, name);
	}

	public String daoPath() {
		String packageName = "com.pcbly.web.persistence." + this.entityL;
		String name = this.entityP + "Dao.java";
		return Constant.getFilePath(packageName, name);
	}

	public Class<?> getClz() {
		return clz;
	}

	public void setClz(Class<?> clz) {
		this.clz = clz;
	}

	public String getEntity() {
		return entity;
	}

	public void setEntity(String entity) {
		this.entity = entity;
	}

	public String getEntityP() {
		return entityP;
	}

	public void setEntityP(String entityP) {
		this.entityP = entityP;
	}

	public String getEntityL() {
		return entityL;
	}

	public void setEntityL(String entityL) {
		this.entityL = entityL;
	}

	public String getFirstEntityL() {
		return firstEntityL;
	}

	public void setFirstEntityL(String firstEntityL) {
		this.firstEntityL = firstEntityL;
	}

	public String getClzName() {
		return clzName;
	}

	public void setClzName(String clzName) {
		this.clzName = clzName;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

}
